package com.dm.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dm.entity.CompanyInfoDO;
import com.dm.entity.PerationLogDo;
import com.dm.entity.SysUserDO;
import com.dm.query.QueryCompanyGroupInfo;
import com.dm.query.QueryCompanyInfo;
import com.dm.service.CompanyGroupInfoService;
import com.dm.service.CompanyInfoService;
import com.dm.service.PerationLogService;
import com.dm.service.SysUserService;
import com.dm.utils.RespsonseResult;


@Controller
@RequestMapping("company")
public class CompanyController {
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	@Autowired
	CompanyGroupInfoService companyGroupInfoService ;
	
	@Autowired
	CompanyInfoService companyInfoService ;
	
	@Autowired
	SysUserService sysUserService ; 
	
	@Autowired
	PerationLogService serationLogService ;
	
	@RequestMapping("selectCompany")
	public @ResponseBody Object selectCompany(HttpServletRequest request,QueryCompanyInfo queryCompanyInfo){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("企业信息查询:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isNotBlank(queryCompanyInfo.getCompanyName())){
				 queryCompanyInfo.setFuzzyCompanyName(queryCompanyInfo.getCompanyName());
				 queryCompanyInfo.setCompanyName(null);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 queryCompanyInfo.setCompanyCode(userInfo.getCompanyInfo());
				 }else{
					 queryCompanyInfo.setGroupCode(userInfo.getCompanyInfo());
				 }
			 }
			 List< CompanyInfoDO >  data = companyInfoService.selectCompanyInfoList(queryCompanyInfo);
			 //查询每个企业的人数
			 if(data!=null && data.size()>0){
				 for(CompanyInfoDO companyInfoDO : data){
					 int count = sysUserService.countCompanyUser(companyInfoDO.getCompanyCode(),null);
					 companyInfoDO.setNumber(count);
				 }
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("addCompany")
	public @ResponseBody Object addCompany(HttpServletRequest request,CompanyInfoDO companyInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("企业信息录入:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isBlank(companyInfoDO.getCompanyName())
					||StringUtils.isBlank(companyInfoDO.getCompanyCode())
					||StringUtils.isBlank(companyInfoDO.getGroupCode())
					||StringUtils.isBlank(companyInfoDO.getLegalPersonName())
					||StringUtils.isBlank(companyInfoDO.getLegalPersonPhone())
					||StringUtils.isBlank(companyInfoDO.getProtectionPersonName())
					||StringUtils.isBlank(companyInfoDO.getProtectionPersonPhone())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 result.setMsg("权限不足");
					 return JSON.toJSON(result);
				 }else if(!userInfo.getCompanyInfo().equals(companyInfoDO.getGroupCode())){
					 result.setMsg("权限不足");
					 return JSON.toJSON(result);
				 }
			 }
			 QueryCompanyInfo queryCompanyInfo =new QueryCompanyInfo();
			 queryCompanyInfo.setCompanyCode(companyInfoDO.getCompanyCode());
			 if(companyInfoService.selectOneCompanyInfo(queryCompanyInfo)!=null){
				 result.setMsg("企业代码已存在");
				 return JSON.toJSON(result);
			 }
			 
			 QueryCompanyGroupInfo queryCompanyGroupInfo = new QueryCompanyGroupInfo();
			 queryCompanyGroupInfo.setGroupCode(companyInfoDO.getGroupCode());
			 if( companyGroupInfoService.selectOneCompanyGroupInfo(queryCompanyGroupInfo)== null){
				 result.setMsg("企业分组不存在");
				 return JSON.toJSON(result);
			 }
			 
			 
			 
			 companyInfoService.insertCompanyInfo(companyInfoDO);
			 result.setMsg("录入成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("upCompany")
	public @ResponseBody Object upCompany(HttpServletRequest request,CompanyInfoDO companyInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("企业信息修改:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isBlank(companyInfoDO.getCompanyCode())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 QueryCompanyInfo codeInfo = new QueryCompanyInfo();
			 codeInfo.setCompanyCode(companyInfoDO.getCompanyCode());
			 CompanyInfoDO data = companyInfoService.selectOneCompanyInfo(codeInfo) ; 
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 result.setMsg("权限不足");
					 return JSON.toJSON(result);
				 }else {
					 if(!userInfo.getCompanyInfo().equals(data.getGroupCode())){
						 result.setMsg("权限不足");
						 return JSON.toJSON(result);
					 }
					 
				 }
			 }
			 companyInfoService.updateCompanyInfoByCompanyId(companyInfoDO);
			 //insertLog(userInfo,data,companyInfoDO,"修改企业信息，企业代码：");
			 result.setMsg("录入成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("录入出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("delCompany")
	public @ResponseBody Object delCompany(HttpServletRequest request,CompanyInfoDO companyInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("企业信息删除:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isBlank(companyInfoDO.getCompanyCode())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 result.setMsg("权限不足");
					 return JSON.toJSON(result);
				 }else {
					 QueryCompanyInfo codeInfo = new QueryCompanyInfo();
					 codeInfo.setCompanyCode(companyInfoDO.getCompanyCode());
					 CompanyInfoDO data = companyInfoService.selectOneCompanyInfo(codeInfo) ; 
					 if(!userInfo.getCompanyInfo().equals(data.getGroupCode())){
						 result.setMsg("权限不足");
						 return JSON.toJSON(result);
					 }
				 }
			 }
			 companyInfoService.deleteCompanyInfoByCompanyId(companyInfoDO);
			 sysUserService.deleteSysUserCompanyInfo(companyInfoDO.getCompanyCode());
			 //insertLog(userInfo,companyInfoDO,null,"删除企业信息，企业代码：");
			 result.setMsg("删除成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	public void insertLog(SysUserDO userInfo,CompanyInfoDO data,CompanyInfoDO companyInfoDO,String msg){
		try {
			 PerationLogDo PerationLogDo = new PerationLogDo();
			 PerationLogDo.setPerationUserId(userInfo.getUserId()+"");
			 PerationLogDo.setPerationUserName(userInfo.getName());
			 if(data!=null){
				 PerationLogDo.setPerationInfoBefore(data.toString());
				 PerationLogDo.setPerationType(msg+data.getCompanyCode());
			 }
			 if(companyInfoDO!=null){
				 PerationLogDo.setPerationInfo(companyInfoDO.toString());
			 }
			 PerationLogDo.setPerationTime(new Timestamp(System.currentTimeMillis()));
			 serationLogService.insert(PerationLogDo);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("修改成功，但是插入日志失败");
		}
	}

}
